function [nFs, mXY, vShut, bReload] = Ellipse(bAutoLoad)
% GalvoScanControl
% Scan script template: Ellipse

% Scan scripts should return four variables (in this order):
%  nFs      The sample rate (1/sec between scan points)
%  mXY      A matrix with two columns in the format [X Y], where
%               X  are x-positions
%               Y  are y-positions
%           The unit of this matrix is millimeters, relative to the origin
%  vShut    Vector with same length as mXY where values indicate shutter
%           open/close state (when a shutter is controlled) or the beam
%           intensity (when the output is directed to an intensity
%           modulated laser module, e.g. Coherent CUBE).
%           Note: Values of vShut should not exceed +/- 5V. Values beyond
%           these limits will be fixed at +/- 5V by the software.
%  bReload  Boolean indicating whether the script should be re-loaded each
%           time before executing. Setting this to true (1) will decrease
%           the repetition rate and should only be used for long programs
%           that are not executed at high frequency.



persistent sPnts sTurns sDur sR1 sR2
bReload = 0;

% Default values
if isempty(sPnts), sPnts = '100'; end
if isempty(sTurns), sTurns = '1'; end
if isempty(sDur), sDur = '1000'; end
if isempty(sR1), sR1 = '1'; end
if isempty(sR2), sR2 = '1'; end

cAnswers = inputdlg({'Number of dwell points', ...
    'Turns', ...
    'Duration (ms)', ...
    'Horizontal radius (mm)', ...
    'Vertical radius (mm)'} , ...
    'Rectangle', ones(1, 5), ...
    {sPnts sTurns sDur sR1 sR2} );
if isempty(cAnswers), return, end

% For some reason, closing the input dialog takes forever.
% Placing this pause here prevents this.
pause(0.1)

sPnts = cAnswers{1};
sTurns = cAnswers{2};
sDur = cAnswers{3};
sR1 = cAnswers{4};
sR2 = cAnswers{5};

% Scan along a circle/ellipsoid trajectory, centered on
% the origin with a fixed round-trip duration
nPnts = str2num(sPnts); % number of dwell points
nTurns = str2num(sTurns);
nDur = str2num(sDur) / 1000; % duration, sec
r1 = str2num(sR1); % horizontal radius
r2 = str2num(sR2); % vertical radius

% Compute other parameters
nShutVolt = 5;
nTheta = linspace(0, 2*pi, nPnts);
vX = r1 * cos(nTheta);
vY = r2 * sin(nTheta);

mXY = [vX(:) vY(:)];
mXY = repmat(mXY, nTurns, 1);
vShut = [0; ones(size(mXY,1),1).*nShutVolt; 0]; % shutter always open but starts/stops closed
mXY = [mXY(1,:); mXY; mXY(end, :)];
nFs = 1 / (nDur / nPnts); % 1/sec, Hz

return